/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.logparser.service.impl;

import cn.icanci.basic.commonutils.ArraysUtil;
import cn.icanci.logparser.entity.SystemBaseLogDetailDO;
import cn.icanci.logparser.service.SystemBaseLogDetailService;
import cn.icanci.logparser.service.SystemBaseLogService;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author icanci
 * @date Created in 2021/04/10 22:37
 * @version 1.0
 */
@Service
public class SystemBaseLogDetailServiceImpl implements SystemBaseLogDetailService {

    @Resource
    private SystemBaseLogService systemBaseLogService;

    /**
     * 保存一条日志记录
     * @param targetLog 需要保存的对象
     * @return 返回保存的结果
     */
    @Override
    public void saveOneRowLog(String targetLog) {
        if (StringUtils.isNotEmpty(targetLog)) {
            String[] split = targetLog.split("\\^");
            SystemBaseLogDetailDO systemBaseLogDetailDO = new SystemBaseLogDetailDO();
            systemBaseLogDetailDO.setModule(ArraysUtil.getIndexValue(split, 0));
            systemBaseLogDetailDO.setCategory(ArraysUtil.getIndexValue(split, 1));
            systemBaseLogDetailDO.setSubCategory(ArraysUtil.getIndexValue(split, 2));
            systemBaseLogDetailDO.setMessage(ArraysUtil.getIndexValue(split, 3));
            systemBaseLogDetailDO.setLevel(Integer.valueOf(ArraysUtil.getIndexValue(split, 4)));
            systemBaseLogDetailDO.setCreateTime(new Date());
            systemBaseLogService.insertOneRow(systemBaseLogDetailDO);
        }
    }
}
